package com.shilei;

import com.shilei.common.Dir;
import com.shilei.common.Group;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 坦克Frame
 * @author: 81271
 * @date: 2020-04-07 21:07
 */
public class TankFrame extends Frame {



    static final int GAME_WIDTH = 1080;
    static final int GAME_HEIGHT = 960;

    GameModel gm = GameModel.getInstance();


    public TankFrame() throws HeadlessException {
        setSize(GAME_WIDTH, GAME_HEIGHT);
        setResizable(false);
        setTitle("tank war");
        addKeyListener(new MyKeyListener());
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        setVisible(true);
    }

    private Image offScreenImage = null;

    @Override
    public void update(Graphics g) {
        if (offScreenImage == null) {
            offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
        }
        Graphics graphics = offScreenImage.getGraphics();
        Color color = graphics.getColor();
        graphics.setColor(Color.BLACK);
        graphics.fillRect(0,0, GAME_WIDTH, GAME_HEIGHT);
        graphics.setColor(color);
        paint(graphics);
        g.drawImage(offScreenImage, 0, 0, null);
    }

    @Override
    public void paint(Graphics g) {
        gm.paint(g);


    }

    class MyKeyListener extends KeyAdapter {
        boolean keyRightBl = false;
        boolean keyLeftBl = false;
        boolean keyUpBl = false;
        boolean keyDownBl = false;


        @Override
        public void keyPressed(KeyEvent e) {

            int key = e.getKeyCode();
            switch (key) {
                case KeyEvent.VK_RIGHT:
                    keyRightBl = true;
                    break;
                case KeyEvent.VK_LEFT:
                    keyLeftBl = true;
                    break;
                case KeyEvent.VK_UP:
                    keyUpBl = true;
                    break;
                case KeyEvent.VK_DOWN:
                    keyDownBl = true;
                    break;

                case KeyEvent.VK_S:
                    gm.save();
                    break;

                case KeyEvent.VK_L:
                    gm.reload();
                    break;


                case KeyEvent.VK_CONTROL:
                    gm.getMainTank().fire();
                    break;
            }
            setMainTankDir();
        }

        @Override
        public void keyReleased(KeyEvent e) {
            int key = e.getKeyCode();
            switch (key) {
                case KeyEvent.VK_RIGHT:
                    keyRightBl = false;
                    break;
                case KeyEvent.VK_LEFT:
                    keyLeftBl = false;
                    break;
                case KeyEvent.VK_UP:
                    keyUpBl = false;
                    break;
                case KeyEvent.VK_DOWN:
                    keyDownBl = false;
                    break;
            }

            setMainTankDir();
        }

        private void setMainTankDir() {
            Tank tank =  gm.getMainTank();
            if (keyRightBl) tank.setDir(Dir.RIGHT);
            if (keyLeftBl) tank.setDir(Dir.LEFT);
            if (keyUpBl) tank.setDir(Dir.UP);
            if (keyDownBl) tank.setDir(Dir.DOWN);


            if (!keyRightBl && !keyLeftBl && !keyUpBl && !keyDownBl) {
                tank.setMoveBl(false);
            } else {
                tank.setMoveBl(true);
            }


        }
    }
}
